package org.example.servletpractice.servlet;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "FromServlet",value = "/fromServlet")
public class FromServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        //获取前端传递的值
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");

        //返回给前端
        PrintWriter writer = response.getWriter();
        writer.println("这是使用Get方式提交的数据\n");
        writer.println("用户名: "+userName);
        writer.println("密码: "+password);
        writer.flush();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        //获取前端传递的值
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String[] values = request.getParameterValues("hobby");
        System.out.println(values[1]);

        StringBuilder stringBuilder =new StringBuilder();
        stringBuilder.append("爱好有:");
        for (String value : values) {
            stringBuilder.append(value+"\b");
        }

        //返回给前端
        PrintWriter writer = response.getWriter();
        writer.println("这是使用Post方式提交的数据\n");
        writer.println("用户名: "+userName);
        writer.println("密码: "+password);
        writer.println(stringBuilder.toString());
        writer.flush();
    }
}
